#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2018-03-06 11:29:23
# 实现斐波拉契数列，也就是  1 1 2 3 5 8 13 ....


class FabonacciNumbers(object):
    """斐波拉契数列生成器，迭代器"""

    def __init__(self, number):
        super(FabonacciNumbers, self).__init__()
        self.number = number
        self.fib_list = [1, 1]
        # 迭代标志位
        self.flag = 0

    def __iter__(self):
        return self

    def getAll(self):
        if self.number == 0:
            return None
        elif self.number == 1 or self.number == 2:
            return self.fib_list[0:self.number]
        elif len(self.fib_list) <= 2:
            for i in range(2, self.number):
                self.fib_list.append(
                    self.fib_list[i - 2] + self.fib_list[i - 1])
            else:
                return self.fib_list
        else:
            return self.fib_list

    def next(self):
        """ 迭代器next方法 """
        try:
            self.getAll()
            next_number = self.fib_list[self.flag]
            self.flag += 1
            return next_number
        except IndexError:
            raise StopIteration()

    def getOne(self, n):
        """ 返回指定位置的元素 """
        if n > self.number:
            return '指定的标号大于初始化时传入的大小'
        else:
            self.getAll()
            return self.fib_list[n - 1]


if __name__ == "__main__":
    fab1 = FabonacciNumbers(4)
    for i in fab1:
        print(i)
